#include "MyIncludes.h"
#include "Dictionary.h"

Dictionary::Dictionary()
{
	root = new Node();
	numOfElements = 0;
}
Dictionary::~Dictionary()
{
	//freeAllElements();
}
Worker * Dictionary::find ( StringKey * key )
{
	Node* node = root->Find (*key);
	if(node == NULL)
	{
		return NULL;
	}
	else
	{
		return node->value;
	}
}
int Dictionary::insert (StringKey * key , Worker * worker)
{
	if(root->Find(*key) != NULL)
	{
		return 0;
	}
	else
	{
		Node* node = new Node(worker,*key);
		root = root->Insert(node);
		numOfElements ++ ;
		return 1;
	}
}
Worker * Dictionary::deleteWorker (StringKey * key) 
{
	if (numOfElements <=0)
		return NULL;
	Worker* worker = NULL;
	root = root->Delete(*key, worker);
	numOfElements --;
	return worker;

}
void Dictionary::printDictionary (const char * fileName) 
{
	char * outputFilename ;
	int oFNsize = strlen (fileName);
	outputFilename = new char [oFNsize+4] ;
	//ofstream file;
	//file.open(fileName , ios::app );
	ofstream file ( fileName , ios::app ) ;
	//Node * temp = root ;
	//printDictionaryHelper(temp);
	if(!(root->IsLeaf()))
	{
		root->printByOrder(file);
	}
	else
		file << "empty\n";
	//root->printNode(0,'R');
	//file.close();
}
void Dictionary::printDictionaryHelper (const Node * node)
{
}
void Dictionary::freeAllElements ()
{
	//delete root;
	if ( numOfElements == 0)
		return;
	numOfElements = 0;
	root->FreeAllElements();
}
